// pages/newaddress/newaddress.js
import myrequest from "../../utils/myrequest"
Page({

  /**
   * 页面的初始数据
   */
 
  data: {
    "provice":{
      proviceName:"",
      cityName:"",
      countName:"",
      detailInfo:"",
      telNumber:"",
      userName:"",
     
    },
    value:"",
    username:"",
    telphone:"",
    detailaddress:""
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
   select_fn(event){
    //  选择城市的函数
    let value = event.detail.value.join(",")
    this.setData({
      value
    })
    console.log(value)
   },
   async save_address(){
    // 地址爆粗你的业务
    // let  {data} = await myrequest({ur:"/saveaddress",method:"POST"})
    // console.log(data)
    let  {value,username,telphone,detailaddress} = this.data
    // 把写好的地址保存到本地服务器
    wx.setStorageSync()
    let {data} = await myrequest({
      url:"/saveaddress",
      method:"POST",
      data:{
        value,username,telphone,detailaddress
      }
    })
   },
   save_fn(event){
    //  保存用户地址星系
    let  {value,username,telphone,detailaddress} = this.data
    // 填完地址返回订单也从订单页再去其他页面再返回地址丢失


    if(value.length==0||username.length==0||detailaddress.length==0||telphone.length==0){
        return wx.showToast({
          title:"信息不对",
          icon:"error"

        }) 
    }
    // 接口请求
    this.save_address()
    wx.navigateTo({
      url: '/pages/order/order?user='+username+"&value="+value+'&phone='+telphone+"&address="+detailaddress,
    })
    
    },
   getuser(event){
     console.log(event)
    //  获取用户名
    this.setData({
      username:event.detail.value
     
    })
    console.log(this.data.username)
    
   },
   getphone(event){
    //  获取手机号
    this.setData({
      telphone:event.detail.value
    })
    console.log(this.data.telphone)
   },
   getaddress(event){
    //  获取i昂西地址
    this.setData({
      detailaddress:event.detail.value
    })  
    console.log(this.data.detailaddress)
   },
   chooseAddress(){
    wx.chooseAddress({
      success(res){
        console.log(res)
        res={

        }
        let {proviceName,cityName,countName,detailInfo,telNumber,userName} = res
        this.setData({
          proviceName,
          cityName,
          countName,
          detailInfo,
          telNumber,
          userName
        })
      }
    })
   },
  onLoad: function (options) {
    console.log(options)
    if(options.user){
      // 从订单页过来带地址
      let value = options.value
      let username = options.user
      let detailaddress = options.address
      let telphone = options.phone
      this.setData({
        value,username,detailaddress,telphone
      })
    }else{
      // url上没有携带用户信息
      let newaddress = wx.getStorageSync('newaddress')
      newaddress = newaddress.split(" ")
      console.log(newaddress)
      this.setData({
        value:'',
        detailaddress:newaddress[0],
        username:newaddress[1],
        telphone:newaddress[2]
      })  
      console.log(newaddress)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})